package com.walleipt.corgi.api.controller;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mangoframework.core.annotation.Get;
import org.mangoframework.core.annotation.Post;
import org.mangoframework.core.annotation.RequestMapping;
import org.mangoframework.core.annotation.RequestParam;
import org.mangoframework.core.dispatcher.Parameter;
import org.springframework.jdbc.core.PreparedStatementSetter;

import com.walle.ak47.commons.utils.ConvertUtils;
import com.walle.ak47.commons.utils.StringUtils;
import com.walle.ak47.commons.utils.UUIDGenerator;
import com.walleipt.corgi.api.annotations.Ignore;
import com.walleipt.corgi.api.common.dao.Page;
import com.walleipt.corgi.api.common.dao.mysql.SmartDao;
import com.walleipt.corgi.api.ex._HashMap;
import com.walleipt.corgi.api.utils.AssertUtils;

@RequestMapping("/registerUserManager")
public class RegisterUserController {

	String tableName = "agent_register_user" ;
	
    @Ignore
    @Post("register")
    public Object register(Parameter parameter) {
    
        AssertUtils.notNull(parameter, "mobile", "password", "parentId" );
        String id = UUIDGenerator.getUUID() ;
        Map<String, Object> attrs = new HashMap<>() ;
        attrs.put( "id", id ) ;
        attrs.put( "mobile", parameter.getParamString().get( "mobile") ) ;
        attrs.put( "password", parameter.getParamString().get( "password") ) ;
        String parentId = parameter.getParamString().get( "parentId") ;
        attrs.put( "parentId",parameter.getParamString().get( "parentId") ) ;
        
        Map<String,Object> userInfo = SmartDao.instance().findById( tableName,  parentId ) ;
        int level = -1 ;
        if ( userInfo!=null )
        	level = ConvertUtils.toInt( userInfo.get( "level" ).toString() ) ; 
        attrs.put( "level", level ) ;
        attrs.put( "flag", 1) ;
        attrs.put( "createTime", new Date() );
        attrs.put( "lastTime", new Date() ) ;
        SmartDao.instance().save_no_id( "agent_register_user", attrs) ;
        return new _HashMap<>().add("id", id) ;
    }
    
    @Get("search")
    public Object search(Parameter parameter) {
    	
        String key = parameter.getParamString().get("key");
        int pageNum = ConvertUtils.toInt(parameter.getParamString().get("pageNum")) ;
        int pageSize = ConvertUtils.toInt(parameter.getParamString().get("pageSize")) ;
        
        Page page = null;
		try {
			List paras = new ArrayList() ; ;
			String sql = "select * from "+tableName+" where flag=1 " ; 
			if (key != null && key.trim().length() > 0)
			{
				sql += " and mobile like '%?%' " ;
				paras.add( key ) ;
			}
			sql += "order by createtime desc " ;
			page = SmartDao.instance().paginateForMysql(pageNum, pageSize, sql, paras.toArray() ) ;
		} catch (Exception e) {
			e.printStackTrace();
		}
        return new _HashMap<>().add("page", page);
    }
    
    @Get("/del/{id}")
    public Object delUserById(@RequestParam("id") String id)
    {
    	Map<String,Object> attrs = new HashMap<>() ;
    	attrs.put( "id", id ) ;
    	attrs.put( "flag", 0 ) ;
		try {
			SmartDao.instance().updateSkipNull(tableName, attrs );
		} catch (Exception e) {
			e.printStackTrace();
		}
    	 
    	 return new _HashMap<>() ;
    }
    
    @Post("/batchDel")
    public Object batchDelUser(Parameter parameter)
    {
    	String ids = parameter.getParamString().get( "ids" ) ;
    	String[] idsArray = StringUtils.split(ids,",");
    	 
    	final List<Object> paras = new ArrayList<Object>();
    	paras.add( new Date() ) ;
    	final StringBuilder sql = new StringBuilder();
		sql.append( "update " ).append(tableName).append(" set flag=0,lastTime=?  where id in (");
		for (String id : idsArray) {
			if (paras.size() > 0)
				sql.append(",");
			
			sql.append("?");
			paras.add( id ) ;
		}
		sql.append(")") ;
		
		int size = 0 ;
		try {
			size = SmartDao.instance().getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
				@Override
				public void setValues(PreparedStatement ps) throws SQLException {
					for (int i = 1; i <= paras.size(); i++) {
						ps.setObject(i, paras.get(i-1));
					}
				}
			});
		} catch (Exception ex) {
			ex.printStackTrace();
		}
    			
    	return new _HashMap<>();
    }

}
